package cn.bs.busuo.account.consumer;

import cn.bs.busuo.account.dao.cache.IUserCacheRepository;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeReturnType;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component
@RocketMQMessageListener(topic = "information-update",consumerGroup = "busuo-consumer-admin-user-account",maxReconsumeTimes = 5)
public class InformationUpdate implements RocketMQListener<Long> {
    @Autowired
    private IUserCacheRepository iUserCacheRepository;
    @Override
    public void onMessage(Long userId) {
        //这里应该接到 你好的消息
        System.out.println("更新信息后需要删除缓存的用户id:"+userId);
        //异步解耦调用删除缓存的方法
        boolean b = iUserCacheRepository.deleteUserBase(userId);
        if(b){
            System.out.println("第二次删除缓存成功");
        }else{
            //如果缓存删除失败则重新尝试5次
            for(int i=0;i<5;i++){
                System.out.println("删除缓存失败，第"+i+1+"次重试");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                boolean b1 = iUserCacheRepository.deleteUserBase(userId);
                if(b1){
                    System.out.println("删除缓存成功");
                    break;
                }
            }
        }
    }
}
